<?php
namespace Common\Mysql;
class MenuMysql extends CommonMysql {

    public function addMenu($data) {
        $title = v($data,'title');
        verify($title,'notEmpty','菜单名称不能为空');

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        $type = v($data,'type');
        if(!in_array($type,array(1,2,3,4))) {
            E('非法菜单类型');
        }

        $value = v($data,'value');
        verify($value,'notEmpty','ID或链接不能为空');

        if($this->where("(`title`='{$title}' AND `sid` = '{$sid}') or (`type` = {$type} AND `value` = '{$value}')")->count()) {
            E('菜单已经存在');
        }

        if($type == 1) {
            if(M('Category')->where(array('sid'=>$sid,'id'=>$value))->count() < 1) {
                E('分类不存在');
            }
        }elseif ($type == 2) {
            if(M('SiteTags')->where(array('sid'=>$sid,'tagid'=>$value))->count() < 1) {
                E('标签不存在');
            }
        }elseif ($type == 3) {
            if(M('Page')->where(array('sid'=>$sid,'id'=>$value))->count() < 1) {
                E('单页不存在');
            }
        }elseif ($type == 4) {
            verify($value,'isUrl','非法链接地址');
        }

        $id = $this->addRecord(array(
            'title'     =>  $title,
            'sid'       =>  $sid,
            'type'      =>  $type,
            'value'     =>  $value,
            'sort'      =>  $sort,
            'createtime'=>  NOW_TIME,
            'updatetime'=>  NOW_TIME,
            'status'    =>  $status,
        ),false,'菜单添加失败');

        return $id;
    }

    public function updateMenu($data) {
        $id = v($data,'id');
        verify($id,'notEmpty','菜单ID不能为空');
        verify($id,'isID','非法菜单ID');

        $title = v($data,'title');
        verify($title,'notEmpty','菜单名称不能为空');

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        $menuInfo = $this->find($id);
        verify($menuInfo,'notEmpty','菜单不存在');

        $type = v($data,'type');
        if(!in_array($type,array(1,2,3,4))) {
            E('非法菜单类型');
        }

        $value = v($data,'value');
        verify($value,'notEmpty','ID或链接不能为空');

        if($this->where("(`title`='{$title}' AND `sid` = '{$sid}' AND `id`<> {$id}) or (`type` = {$type} AND `value` = '{$value}' AND `id`<> {$id})")->count()) {
            E('菜单已经存在');
        }

        if($type == 1) {
            if(M('Category')->where(array('sid'=>$sid,'id'=>$value))->count() < 1) {
                E('分类不存在');
            }
        }elseif ($type == 2) {
            if(M('SiteTags')->where(array('sid'=>$sid,'tagid'=>$value))->count() < 1) {
                E('标签不存在');
            }
        }elseif ($type == 3) {
            if(M('Page')->where(array('sid'=>$sid,'id'=>$value))->count() < 1) {
                E('单页不存在');
            }
        }elseif ($type == 4) {
            verify($value,'isUrl','非法链接地址');
        }

        $id = $this->updateRecord(array(
            'id'        =>  $id,
            'title'     =>  $title,
            'type'      =>  $type,
            'sid'       =>  $sid,
            'value'     =>  $value,
            'sort'      =>  $sort,
            'status'    =>  $status,
        ),false,'菜单修改失败');

        return $id;
    }
}
?>
